package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTradeMarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import springfox.documentation.spring.web.json.Json;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/7/7 16:01
 * @Version 1.0
 */
@Service
public class BaseCategoryTradeMarkServiceImpl implements BaseCategoryTradeMarkService {

    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        //  1. 根据分类Id 查询品牌Id; base_category_trademark
        LambdaQueryWrapper<BaseCategoryTrademark> trademarkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        trademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        List<BaseCategoryTrademark> categoryTrademarkList = baseCategoryTrademarkMapper.selectList(trademarkLambdaQueryWrapper);
        //        //  声明一个tmId 集合
        //        ArrayList<Long> tmIdList = new ArrayList<>();
        //        //  获取到tmId 集合
        //        categoryTrademarkList.forEach(baseCategoryTrademark -> {
        //            tmIdList.add(baseCategoryTrademark.getTrademarkId());
        //        });
        if (!CollectionUtils.isEmpty(categoryTrademarkList)){
            //  map 1,3,2 存储拉姆达表达式。
            //  List<Long> idList = categoryTrademarkList.stream().map(baseCategoryTrademark -> baseCategoryTrademark.getTrademarkId()).collect(Collectors.toList());

            //  方法引用 1,3,2  :: 等价于 baseCategoryTrademark -> baseCategoryTrademark.getTrademarkId() 并返回
            List<Long> tmIdList = categoryTrademarkList.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());

            //  2. 根据品牌Id 获取品牌集合数据! base_trademark
            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectBatchIds(tmIdList);
            return baseTrademarkList;
        }
        //   返回寂寞
        return null;
    }

    @Override
    public void remove(Long category3Id, Long trademarkId) {
        //  删除数据
        LambdaQueryWrapper<BaseCategoryTrademark> trademarkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        trademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        trademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getTrademarkId,trademarkId);
        baseCategoryTrademarkMapper.delete(trademarkLambdaQueryWrapper);
    }

    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {
        //  {"category3Id":67,"trademarkIdList":[1,2,3]} Json 变为实体类：
        //  获取到品牌Id 集合
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        if (!CollectionUtils.isEmpty(trademarkIdList)){
            trademarkIdList.forEach(tmId->{
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setTrademarkId(tmId);
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                //  保存数据 for --- insert
                baseCategoryTrademarkMapper.insert(baseCategoryTrademark);
            });
        }
    }

    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
        //  需要知道已绑定的品牌
        LambdaQueryWrapper<BaseCategoryTrademark> trademarkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        trademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        List<BaseCategoryTrademark> categoryTrademarkList = baseCategoryTrademarkMapper.selectList(trademarkLambdaQueryWrapper);
        //  判断
        if (!CollectionUtils.isEmpty(categoryTrademarkList)){
            //  获取到已绑定的匹配Id 集合
            //  从循环中获取一个数据，然后将这个数据放入集合中，此时就可以使用stream 流. 1,2,3
            List<Long> tmIdList = categoryTrademarkList.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());

            //  我需要知道所有的品牌 1,2,3,5,6
            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectList(null);
            List<BaseTrademark> trademarkList = baseTrademarkList.stream().filter(baseTrademark -> !tmIdList.contains(baseTrademark.getId())).collect(Collectors.toList());
            //            List<Long> idList = baseTrademarkList.stream().map(BaseTrademark::getId).collect(Collectors.toList());
            //            for (Long aLong : idList) {
            //                for (Long aLong1 : tmIdList) {
            //                    if (aLong1.longValue() != aLong.longValue()){
            //                         aLong1
            //                    }
            //                }
            //            }
            return trademarkList;
        }
        //  返回全部品牌数据
        return baseTrademarkMapper.selectList(null);
    }
}
